// miniprogram/pages/signin/signin.js
const app = getApp()
//用于存储用户输入的个人信息
//用于存储用户唯一的OpenID
var OpenID = getApp().globalData.openid;

Page({
  mixins: [require('../../mixin/themeChanged')],
  /**
   * 页面的初始数据
   */
  data: {
    isSetInfo:false,
    showTopTips: false,
    id_number:null,
    openid:null,
    room_id:"",
    phone:null,
    year:"",
    name:"",
    sex:"男",
    health:"",
    //用户个人信息
    userInfo: {
      avatarUrl: "", //用户头像
      nickName: "", //用户昵称
    },
    //专业列表
    majors: [
     ["健康","乏力", "咳嗽", "发烧", "疑似新冠状病毒感染", "确诊新冠状病毒感染"],
     ["男","女"]
    ],
    majorIndex: 0,
    sexIndex:0,
    idErr: false,
    nameErr: false,
    classErr: false,
    ErrMessage: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    /**
     * 获取用户信息
     */
    wx.getUserInfo({
        success: function (res) {
          console.log(res);
          var avatarUrl = 'userInfo.avatarUrl';
          var nickName = 'userInfo.nickName';
          that.setData({
            [avatarUrl]: res.userInfo.avatarUrl,
            [nickName]: res.userInfo.nickName,
          })
          that.setData({
            isSetInfo:getApp().globalData.isSetInfo,
            openid:getApp().globalData.openid,
            id_number:getApp().globalData.id_number,
            room_id:getApp().globalData.room_id,
            phone:getApp().globalData.phone,
            year:getApp().globalData.year,
            name:getApp().globalData.name,
            health:getApp().globalData.health,
            sex:getApp().globalData.sex,
          })
          console.log("change:"+that.data.room_id)
        }
      })
  },


  // 健康内容的更变
  bind_health: function (e) {
    console.log(e)
    this.setData({
      majorIndex: e.detail.value
    })
    // 每次更变记录选择的专业，存入inputInfo对象
    this.setData({
      health:this.data.majors[0][this.data.majorIndex]
    })
    console.log('健康发生选择改变，携带值为', this.data.health);
  },
 // 性别内容的更变sex
 bind_sex: function (e) {
  console.log(e)
  this.setData({
    sexIndex: e.detail.value
  })
  // 每次更变记录选择的专业，存入inputInfo对象
  this.setData({
    sex:this.data.majors[1][this.data.sexIndex]
  })
  console.log('性别发生选择改变，携带值为', this.data.sex);
},
  // 响应身份证号输入内容的更变
  bindId_number: function (e) {
    console.log('身份证输入完成，携带值为', e.detail.value);
    // 每次更变记录输入的学号，存入inputInfo对象
    this.setData({
      id_number: e.detail.value
    })
    console.log("ide_number"+this.data.id_number)
  },
   // 电话号码输入内容的更变
   bind_phone: function (e) {
    console.log('电话号码输入完成，携带值为', e.detail.value);
    // 每次更变记录输入的学号，存入inputInfo对象
    this.setData({
      phone: e.detail.value
    })
    console.log("phone"+this.data.id_number)
  },
  //绑定年龄
  bind_year: function (e) {
    console.log('年龄输入完成，携带值为', e.detail.value);
    // 每次更变记录输入的学号，存入inputInfo对象
    this.setData({
      year: e.detail.value
    })
    console.log("ide_number"+this.data.year)
  },
  //绑定姓名
  bind_name: function (e) {
    console.log('姓名输入完成，携带值为', e.detail.value);
    // 每次更变记录输入的学号，存入inputInfo对象
    this.setData({
      name: e.detail.value
    })
    console.log("ide_number"+this.data.name)
  },
//绑定房间
bind_room_id: function (e) {
  console.log('房间输入完成，携带值为', e.detail.value);
  // 每次更变记录输入的学号，存入inputInfo对象
  this.setData({
    room_id: e.detail.value
  })
  console.log("ide_number"+this.data.room_id)
},

  /**
   * 确定按钮
   */
  btnclick: function (e) {
  //  判断学号输入是否正确
  var that = this;
   var size ;
   size = this.data.id_number;
   console.log(this.data.id_number)
    if (this.data.id_number==null) {
      this.setData({
        idErr: true
      });
      console.log("身份证号输入错误");
    }else {
      console.log("正确")
      this.setData({
        idErr: false
      });
    }
    // 判断姓名输入是否正确
    if (this.data.name == "") {
      this.setData({
        nameErr: true
      });
      console.log("姓名未填写完整");
    } else {
      this.setData({
        nameErr: false
      });
    }
    // 判断号码输入是否正确
    var ph  = "";
    ph= this.data.phone;
    console.log(ph)
      if(ph==null){
      this.setData({
         classErr: true
      });
      console.log("电话号码有误");
    } else {
      this.setData({
        classErr: false
      });
    }
  //   // 判断信息是否填写有误，有误则提示，同时不上传数据
    if (this.data.idErr || this.data.nameErr || this.data.classErr) {
      this.setData({
        ErrMessage: "个人信息填写有误或不完整"
      })
      return
   }
   wx.request({
    // url:"http://localhost:8080/set_userInfo",
    url:"http://59.110.42.131:8080/set_userInfo",
     method:"POST",
     header: {
      'content-type': 'application/x-www-form-urlencoded'
    },
    data:{
      openid:getApp().globalData.openid,
      id_number:that.data.id_number,
      phone:that.data.phone,
      room_id:that.data.room_id,
      name:that.data.name,
      year:that.data.year,
      sex:that.data.sex,
      health:that.data.health,
    },
    success:function(res){
      if(res.data.status = 200){
        getApp().globalData.openid=res.data.data.openid,
        getApp().globalData.room_id = res.data.data.room_id,
        getApp().globalData.id_number = res.data.data.id_number,
        getApp().globalData.phone = res.data.data.phone,
        getApp().globalData.year = res.data.data.year,
        getApp().globalData.name= res.data.data.name,
        getApp().globalData.health = res.data.data.health, 
        getApp().globalData.sex = res.data.data.sex,
        getApp().globalData.isSetInfo = true,
        console.log("成功"+res.data.data)
      }
    },
    fail:function(res){
        console.log("失败")
    }
   })
       // 进入主界面
    wx.switchTab({
      url: '/pages/PersonalCenter/PersonalCenter'
    })
  }
  })